package com.drug.lib.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.LayoutDirection;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.drug.lib.R;
import com.flyco.roundview.RoundLinearLayout;

/**
 * 创建日期：2020/7/3 on 14:46
 * 描述:自定义输入框
 * 作者:zhq Administrator
 */
public class TextInputPanel extends LinearLayout {

    private TextView leftTextView;
    private EditText inputText;
    private TextView inputTextX;
    private ImageView arrowDown;
    private RelativeLayout inputBox;
    private LinearLayout rightView;
    private LinearLayout leftLayout;
    private RoundLinearLayout container;
    private View inputLine;
    private TextView unitText;
    private ImageView mandatoryIdentification;
    private ImageView leftImage;
    private int mRightImage;
    private int mReftImage;
    private boolean mRightShow;
    private String leftTitle;
    private String title;
    private String titleHint;
    private int titleColor;
    private int backgroundColor;
    private int leftColor;
    private int lineColor;
    private int titlePosition;
    private boolean isEdit;
    private boolean isLine;
    private String unit;
    private int unitColor;
    private int borderColor;
    private boolean showIdentify;
    private boolean showLeft;
    private boolean showRight;
    private Context mContext;

    public TextInputPanel(Context context) {
        super(context);
        this.mContext = context;
    }


    public TextInputPanel(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView(context);
        configurationAttributes(context, attrs);
    }

    public TextInputPanel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView(context);
        configurationAttributes(context, attrs);
    }

    private void configurationAttributes(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.text_input_panel);
        mRightImage = typedArray.getResourceId(R.styleable.text_input_panel_input_image_arrow, R.mipmap.xyfj_arrow_grad_icon);
        mReftImage = typedArray.getResourceId(R.styleable.text_input_panel_input_left_image, 0);
        mRightShow = typedArray.getBoolean(R.styleable.text_input_panel_input_show_arrow, false);
        leftTitle = typedArray.getString(R.styleable.text_input_panel_input_left_title);
        title = typedArray.getString(R.styleable.text_input_panel_input_title_text);
        titleHint = typedArray.getString(R.styleable.text_input_panel_input_title_hint);
        isEdit = typedArray.getBoolean(R.styleable.text_input_panel_input_is_edit, false);
        leftColor = typedArray.getColor(R.styleable.text_input_panel_input_left_color, ContextCompat.getColor(getContext(), R.color.default_text_color));
        titleColor = typedArray.getColor(R.styleable.text_input_panel_input_title_color, ContextCompat.getColor(getContext(), R.color.text_color_lb));
        backgroundColor = typedArray.getColor(R.styleable.text_input_panel_input_background, ContextCompat.getColor(getContext(), R.color.color_transparent));
        lineColor = typedArray.getColor(R.styleable.text_input_panel_input_line_bg, ContextCompat.getColor(getContext(), R.color.content_bg_color));
        isLine = typedArray.getBoolean(R.styleable.text_input_panel_input_show_line, false);
        titlePosition = typedArray.getInteger(R.styleable.text_input_panel_input_title_position, 0);
        unit = typedArray.getString(R.styleable.text_input_panel_input_right_unit);
        unitColor = typedArray.getColor(R.styleable.text_input_panel_input_unit_color, ContextCompat.getColor(getContext(), R.color.text_color_lb));
        borderColor = typedArray.getColor(R.styleable.text_input_panel_input_border_color, ContextCompat.getColor(getContext(), R.color.color_transparent));
        showIdentify = typedArray.getBoolean(R.styleable.text_input_panel_input_show_identify, false);
        showLeft = typedArray.getBoolean(R.styleable.text_input_panel_input_show_left, true);
        showRight = typedArray.getBoolean(R.styleable.text_input_panel_input_show_right, true);
        typedArray.recycle();
    }

    private void initView(Context context) {
        View.inflate(context, R.layout.view_input_text_ui, this);
        arrowDown = findViewById(R.id.ui_arrow_down);
        inputText = findViewById(R.id.input_text_ui);
        inputTextX = findViewById(R.id.input_text_x);
        leftTextView = findViewById(R.id.left_text_title);
        inputBox = findViewById(R.id.text_input_box);
        inputLine = findViewById(R.id.input_line);
        unitText = findViewById(R.id.text_unit);
        rightView = findViewById(R.id.right_view);
        container = findViewById(R.id.container_ll);
        mandatoryIdentification = findViewById(R.id.mandatory_identification);
        leftImage = findViewById(R.id.left_image);
        leftLayout = findViewById(R.id.left_layout);
    }

    public TextView getInputEditText() {
        return isEdit ? inputText : inputTextX;
    }

    public EditText getEditText() {
        return inputText;
    }

    public View getInputBox() {
        return inputBox;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (mReftImage != 0) {
            leftImage.setImageResource(mReftImage);
        }
        setArrowDown(mRightImage);
        showArrowDown(mRightShow);
        setShowIdentify(showIdentify);

        leftTextView.setTextColor(leftColor);
        unitText.setTextColor(unitColor);
        inputText.setTextColor(titleColor);
        inputTextX.setTextColor(titleColor);
        inputLine.setBackgroundColor(lineColor);
        container.getDelegate().setBackgroundColor(backgroundColor);
        container.getDelegate().setStrokeColor(borderColor);

        inputLine.setVisibility(isLine ? VISIBLE : GONE);
        setLeftTextTitle(leftTitle);
        setShowLeft(showLeft);
        setShowRight(showRight);
        setInputHint(titleHint);
        setInputText(title);
        setUnitText(unit);
        setIsEdit(isEdit);
        setTitlePosition(titlePosition);
    }

    public void setShowIdentify(boolean showIdentify) {
        this.showIdentify = showIdentify;
        mandatoryIdentification.setVisibility(showIdentify ? VISIBLE : GONE);
    }

    public String getInputText() {
        if (inputText.getVisibility() == View.VISIBLE) {
            return inputText.getText().toString().trim();
        } else {
            return inputTextX.getText().toString().trim();
        }
    }

    public void setLeftTextTitle(String title) {
        if (!TextUtils.isEmpty(title))
            leftTextView.setText(title);
        leftTextView.setVisibility(TextUtils.isEmpty(title) ? GONE : VISIBLE);
    }

    public void setShowLeft(boolean showLeft) {
        this.showLeft = showLeft;
        leftLayout.setVisibility(showLeft ? VISIBLE : GONE);
    }

    public void setShowRight(boolean showRight) {
        this.showRight = showRight;
        rightView.setVisibility(showRight ? VISIBLE : GONE);
    }

    public void setInputText(String text) {
        if (isEdit) {
            inputText.setText(text);
            if (!TextUtils.isEmpty(text)) {
                inputText.setSelection(text.length());
            }
        } else {
            inputTextX.setText(text);
        }
    }

    public void setUnitText(String unit) {
        unitText.setText(unit);
        requestLayout();
    }

    public void setInputHint(String hint) {
        inputText.setHint(hint);
        inputTextX.setHint(hint);
    }

    @SuppressLint("WrongConstant")
    public void setTitlePosition(int position) {
        switch (position) {
            case 0:
                inputText.setLayoutDirection(LayoutDirection.LTR);
                inputTextX.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                break;
            case 1:
                inputText.setLayoutDirection(LayoutDirection.RTL);
                inputTextX.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
                break;
            case 2:
                inputText.setGravity(Gravity.CENTER);
                inputTextX.setGravity(Gravity.CENTER);
                break;
        }

    }

    public void setIsEdit(boolean isEdit) {
        this.isEdit = isEdit;
        inputTextX.setVisibility(isEdit ? GONE : VISIBLE);
        inputText.setVisibility(isEdit ? VISIBLE : GONE);
    }

    public void setArrowDown(int res) {
        if (res != 0) {
            this.mReftImage = res;
            arrowDown.setImageResource(res);
        }
    }

    public void showArrowDown(boolean mRightShow) {
        this.mRightShow = mRightShow;
        arrowDown.setVisibility(mRightShow ? VISIBLE : GONE);
        requestLayout();
    }


    public void setArrowDownClick(OnClickListener onClickListener) {
        if (onClickListener != null)
            rightView.setOnClickListener(onClickListener);
    }

    public void setOnFocusChangeListener(OnFocusChangeListener onFocusChangeListener, String tag) {
        if (onFocusChangeListener != null) {
            inputText.setOnFocusChangeListener(onFocusChangeListener);
            inputText.setTag(tag);
        }
    }

    public void setOnFocusChangeListener(OnFocusChangeListener onFocusChangeListener) {
        if (onFocusChangeListener != null) {
            inputText.setOnFocusChangeListener(onFocusChangeListener);
        }
    }
}